from flask import current_app, jsonify
from flask import g
from flask import render_template
from flask import request

from info import db
from info.common import user_login_data
from info.food_models import OrderList
from info.modules.admin_order import admin_order_blu


@admin_order_blu.route("/order")
def admin_order():
    page = request.args.get('p', 1)

    try:
        page = int(page)
    except Exception as e:
        current_app.logger.error(e)
        page = 1

    order_list = []
    current_page = 1
    total_page = 1
    try:
        paginate = OrderList.query.order_by(OrderList.create_time.desc()).paginate(page,10,False)
        order_list = paginate.items
        current_page = paginate.page
        total_page = paginate.pages
    except Exception as e:
        current_app.logger.error(e)

    order_dict_list = []

    for order in order_list:
        order_dict_list.append(order.to_dict())

    data = {'total_page': total_page,
            'current_page': current_page,
            'order_list': order_dict_list,
           }

    return render_template("admin/order.html",data=data)


@admin_order_blu.route("/del_order/<int:order_id>")
def admin_del_order(order_id):

    order_list = None
    try:
        order_list = OrderList.query.filter(OrderList.id == order_id).first()
    except Exception as e:
        current_app.logger.error(e)

    try:
        db.session.delete(order_list)
        db.session.commit()
    except Exception as e:
        db.session.roolback()
        current_app.logger.error(e)

    return jsonify(errno="0", errmsg="操作成功")




